package mini.reanimate;

import static bogus.core.BogusCore.egl;

import java.io.FileNotFoundException;

import com.jogamp.opengl.GL3;

import owg.util.Calc;
import owg.util.euclidian.Compass;
import owg.util.opengl.BlendMode;
import owg.util.opengl.ColorF;
import owg.util.opengl.ShaderProgram;
import world.Global;
import world.Sound;
import hubol.Hubol;
import hubol.Shake;
import hubol.Sprite;

public class Borgus{
	public float scale = .4f;
	private final Shake[] s = Shake.shake(5);
	
	private final Sprite sprite = new Sprite("borgus"), arrival = new Sprite("borgusarrival"), pencil = new Sprite("pencil");
	
	private float anim = 0, goal = (float)Calc.random(5);
	private float in = 0;
	
	private int claw = 0;
	
	private double x, y;
	
	private ShaderProgram p;
	
	private int timer;
	
	public Borgus(double x, double y){
		this.x = x;
		this.y = y;
		
		timer = -(int)Calc.random(120);
		
	    try {
			p = Hubol.loadProgram(null, Global.streamRelative("res/shaders/alphaTest.frag"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	    
	    Sound.play("borgusappear");
		
		sprite.setOrigin(Compass.SOUTH);
		arrival.setOrigin(Compass.SOUTH);
		pencil.setOrigin(Compass.SOUTHWEST);
	}
	
	public void step(){
		anim += .01;
		if (anim >= goal){
			anim = 0;
			goal = (float)Calc.random(5);
		}
		
		timer++;
		if (timer == 0){
			Sound.playPitched("claw");
			claw = 1;
		}
		else if (timer == 6)
			claw = 0;
		else if (timer == 24){
			Sound.playPitched("claw");
			claw = 1;
		}
		else if (timer == 30){
			claw = 0;
			timer = -80 -(int)Calc.random(180);
		}
		
		if (in < 1){
			in += .005;
			in *= 1.05;
		}
		if (in > 1)
			in = 1;
		
		/*if (Input.checkAnyKey())
			in = 0;*/
	}
	
	public void render(){
		boolean partial = in < 1;
		
		GL3 gl = egl.getGL();
		if (partial){
			egl.useProgram(p);
			p.uniform1f("threshold", .0025f);
			gl.glClearStencil(0);
			gl.glClear( GL3.GL_STENCIL_BUFFER_BIT );
			gl.glEnable( GL3.GL_STENCIL_TEST );
	
			gl.glStencilFunc( GL3.GL_ALWAYS, 1, 0xFF );
			gl.glStencilOp( GL3.GL_KEEP, GL3.GL_KEEP, GL3.GL_REPLACE );
		
			//cut holes in a metaphorical board
			arrival.render(0, x + s[0].x, y + s[0].y + (20 * scale), scale, (float)(1.4166666666666666666666666666667 * in) * scale, 0, ColorF.WHITE); 
	
			gl.glStencilFunc( GL3.GL_EQUAL, 1, 0xFF );
			gl.glStencilOp( GL3.GL_KEEP, GL3.GL_KEEP, GL3.GL_KEEP );
			 
			egl.useProgram(egl.defaultShaderProgram);
		}

		//in essence, spray paint the metaphorical board with the holes
		if (partial)
			egl.setBlendMode(BlendMode.ADD);
		sprite.render(0, x + s[1].x, y + s[1].y, scale, scale, 0, ColorF.WHITE);
		sprite.render(1 + claw, x + s[1].x, y + s[1].y, scale, scale, 0, ColorF.WHITE);
		sprite.render(3, x + s[2].x + (1.2 * Math.cos(anim * 3)), y + s[2].y + (3.1 * Math.sin(Math.pow(anim, 3) * 1.09)), scale, scale, 0, ColorF.WHITE);
		if (partial){
			egl.setBlendMode(BlendMode.NORMAL_PRE);
			ColorF c = new ColorF(ColorF.WHITE, 1/*(float)Math.pow(in, 1.5)*/);
			sprite.render(0, x + s[1].x, y + s[1].y, scale, scale, 0, c);
			sprite.render(1 + claw, x + s[1].x, y + s[1].y, scale, scale, 0, c);
			sprite.render(3, x + s[2].x + (1.2 * Math.cos(anim * 3)), y + s[2].y + (3.1 * Math.sin(Math.pow(anim, 3) * 1.09)), scale, scale, 0, c);
		}
		egl.setBlendMode(BlendMode.ADD);
		sprite.render(4, x + s[3].x, y + s[3].y + (3.1 * Math.sin(Math.pow(anim, 3) * .01)), scale, scale, 0, new ColorF(ColorF.WHITE, (float)(.3 + Math.pow(anim, 4))));
		egl.setBlendMode(BlendMode.NORMAL_PRE);
		
		if (partial){
			gl.glDisable( GL3.GL_STENCIL_TEST );
			final double yy = (20 - (in * 850.0));
			double mult = 128;
			if (yy <= -180 && yy >= -498)
				mult = 185;
			mult *= scale;
			pencil.render(0, x + (mult * Math.cos(anim * Math.PI * 18)), y + yy * scale, scale, scale, 0, ColorF.WHITE);
		}
		
	}

}
